package com.freely.web.action;

import java.io.IOException;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.freely.domain.Message;
import com.freely.domain.User;
import com.freely.service.ChatGroupService;
import com.freely.service.MessageService;
import com.freely.service.UserService;
import com.freely.util.HttpUtil;
import com.freely.util.ResponseMessageUtil;
import com.opensymphony.xwork2.ActionSupport;

public class MessageAction extends ActionSupport{
	private MessageService messageService;
	private UserService userService;
	private ChatGroupService chatGroupService;
	
	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public void setChatGroupService(ChatGroupService chatGroupService) {
		this.chatGroupService = chatGroupService;
	}
	/**
	 * 查看该用户收到的所有消息通知
	 * @throws IOException
	 */
	public void queryAllMessage() throws IOException {
		JSONObject object = HttpUtil.parseJSON("无效的的数据");
		if(object == null) {
			return;
		}
		Long user_id = object.getLong("user_id");
		if(user_id == null) {
			ResponseMessageUtil.response(false, "无效的数据");
			return;
		}
		// 查询是否存在该用户
		DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
		criteria.add(Restrictions.eq("user_id", user_id));
		List<User> result = userService.findByCondition(criteria);
		if(result == null) {
			ResponseMessageUtil.response(false, "不存在该用户");
			return;
		}
		User user = result.get(0);
		// 判断该用户是否被禁用
		if(user.getUser_forbidden()) {
			ResponseMessageUtil.response(false, "该用户被禁用，无法查看通知");
			return;
		}
		// 获取该用户收到的所有消息通知
		Set<Message> messages = user.getMessages();
		if(messages == null || messages.size() <= 0) {
			ResponseMessageUtil.response(false, "该用户暂未收到任何通知");
			return;
		}
		ResponseMessageUtil.response(true, JSON.toJSONString(messages));
	}
	
	/**
	 * 处理同意或拒绝群邀请通知
	 * @throws IOException 
	 */
	public String dealGroupInvite() throws IOException {
		JSONObject object = HttpUtil.parseJSON("无效的的数据");
		if(object == null) {
			return null;
		}
		// 判断数据是否符合规则
		Long user_id = object.getLong("user_id");
		Long group_id = object.getLong("group_id");
		Long message_id = object.getLong("message_id");
		Integer message_result = object.getInteger("message_result");
		if(user_id == null || group_id == null || message_id == null || message_result == null) {
			ResponseMessageUtil.response(false, "无效的数据");
			return null;
		}
		// 判断是否存在该消息通知
		DetachedCriteria criteria = DetachedCriteria.forClass(Message.class);
		criteria.add(Restrictions.eq("message_id", message_id));
		List<Message> result = messageService.findByCondition(criteria);
		if(result == null) {
			ResponseMessageUtil.response(false, "不存在该通知,操作失败");
			return null;
		}
		Message message = result.get(0);
		// 判断该消息是否是该用户与该群
		if(!message.getReceiver().getUser_id().equals(user_id) ||
				!message.getGroup().getGroup_id().equals(group_id)) {
			ResponseMessageUtil.response(false, "错误的操作");
			return null;
		}
		// 判断该用户或群是否被禁用
		if(message.getReceiver().getUser_forbidden()) {
			ResponseMessageUtil.response(false, "您已被禁用，操作失败");
			return null;
		}
		if(message.getGroup().getGroup_forbidden()) {
			ResponseMessageUtil.response(false, "该群已被禁用，操作失败");
			return null;
		}
		// 配置更新语句
		JSONObject modifyObj = new JSONObject();
		modifyObj.put("message_result", message_result);
		modifyObj.put("message_read", true);
		JSONObject conditionObj = new JSONObject();
		conditionObj.put("message_id", message_id);
		// 更新消息通知状态
		try {
			int nums = messageService.update(modifyObj,conditionObj);
		}catch(Exception e) {
			e.printStackTrace();
			ResponseMessageUtil.response(false, "操作失败");
			return null;
		}
		// 操作群邀请结果
		return "agreeJoinGroup";
	}
	
	/**
	 * 处理同意或拒绝群加入邀请
	 */
	public void dealJoinGroupInvite() {
		
	}
}
